import { Injectable } from '@nestjs/common';
@Injectable()
export class ApAntService {
  public codeMessage = {
    code: 200,
    message: '获取成功',
  };
  num = 0;
  modelList = ['standby', 'collect', 'track', 'search'];
  public commInfo: any = [
    {
      id: 0,
      status: {
        id: 0,
        mode: this.modelList[Math.floor(Math.random() * this.modelList.length)],
        status: 'normal',
        buc_warn: 'abnormal',
        bdc_warn: 'abnormal',
        type: '0.9double_paraboloid',
        temp: 0,
        elevate: 0,
        azimuth: 0,
        inertial_navigate_warn: 'abnormal',
        overtemp_warn: 'abnormal',
        transmit_front_status_1: 'abnormal',
        transmit_front_status_2: 'abnormal',
        transmit_front_status_3: 'abnormal',
        transmit_front_status_4: 'abnormal',
        receive_front_status_1: 'abnormal',
        receive_front_status_2: 'abnormal',
        receive_front_status_3: 'abnormal',
        receive_front_status_4: 'abnormal',
        ss: 80,
        sinr: 90,
        sat: 50,
      },
      param: {
        id: 0,
        transmit_switch: 'on',
        receive_switch: 'on',
        track_switch: 'on',
        transmit_frequency: 22500,
        receive_frequency: 22500,
        transmit_polarization: 'right',
        receive_polarization: 'right',
        buc: 15,
        bdc: 0,
      },
      communication: {
        id: 0,
        name: 'ant1',
        ip_addr: '192.168.1.2',
        ip_port: 8080,
        local_port: 6665,
        serial_name: 'ttyS0',
        serial_baud: 115200,
        enable: 'on',
        status: 'on',
        cp_device: 'cp1',
      },
      kd: '可动11111111',
      ant_lock_flag: 1,
    },
    {
      id: 1,
      status: {
        id: 0,
        mode: this.modelList[Math.floor(Math.random() * this.modelList.length)],
        status: 'normal',
        buc_warn: 'abnormal',
        bdc_warn: 'abnormal',
        type: '0.9double_paraboloid',
        temp: 0,
        elevate: 0,
        azimuth: 0,
        inertial_navigate_warn: 'abnormal',
        overtemp_warn: 'abnormal',
        transmit_front_status_1: 'abnormal',
        transmit_front_status_2: 'abnormal',
        transmit_front_status_3: 'abnormal',
        transmit_front_status_4: 'abnormal',
        receive_front_status_1: 'abnormal',
        receive_front_status_2: 'abnormal',
        receive_front_status_3: 'abnormal',
        receive_front_status_4: 'abnormal',
        ss: 80,
        sinr: 90,
        sat: 50,
      },
      param: {
        id: 0,
        transmit_switch: 'on',
        receive_switch: 'on',
        track_switch: 'on',
        transmit_frequency: 22500,
        receive_frequency: 22500,
        transmit_polarization: 'right',
        receive_polarization: 'right',
        buc: '15.00',
        bdc: '0.00',
      },
      communication: {
        id: 0,
        name: 'ant1',
        ip_addr: '192.168.1.2',
        ip_port: 8080,
        local_port: 6665,
        serial_name: 'ttyS0',
        serial_baud: 115200,
        enable: 'on',
        status: 'on',
        cp_device: 'cp2',
      },
      kd: '可动222222',
      ant_lock_flag: 1,
    },
  ];

  getAnt() {
    this.num++;
    return [
      {
        id: 0,
        status: {
          id: 0,
          // 双数on,偶数off
          mode: this.num % 2 === 0 ? 'on' : 'off',
          status: 'normal',
          buc_warn: 'abnormal',
          bdc_warn: 'abnormal',
          type: '0.9double_paraboloid',
          temp: 0,
          elevate: 0,
          azimuth: 0,
          inertial_navigate_warn: 'abnormal',
          overtemp_warn: 'abnormal',
          transmit_front_status_1: 'abnormal',
          transmit_front_status_2: 'abnormal',
          transmit_front_status_3: 'abnormal',
          transmit_front_status_4: 'abnormal',
          receive_front_status_1: 'abnormal',
          receive_front_status_2: 'abnormal',
          receive_front_status_3: 'abnormal',
          receive_front_status_4: 'abnormal',
        },
        param: {
          id: 0,
          transmit_switch: 'on',
          receive_switch: 'on',
          track_switch: 'on',
          transmit_frequency: 22500,
          receive_frequency: 22500,
          transmit_polarization: 'right',
          receive_polarization: 'right',
          buc: 15.0,
          bdc: 0.0,
        },
        communication: {
          id: 0,
          name: 'ant1',
          ip_addr: '192.168.1.2',
          ip_port: 8080,
          local_port: 6665,
          serial_name: 'ttyS0',
          serial_baud: 115200,
          enable: 'on',
          status: 'on',
        },
        kd: '可动1',
      },
    ];
  }

  setCommInfo() {
    this.commInfo.forEach((item) => {
      item.status.mode =
        this.modelList[Math.floor(Math.random() * this.modelList.length)];
    });
    return this.commInfo;
  }

  async postCommGet() {
    return {
      code: 200,
      message: '获取成功',
      data: {
        ant: this.setCommInfo(),
      },
    };
  }

  async postCommSet(body: any) {
    const index = this.commInfo.findIndex((item) => item.id === body.id);
    this.commInfo[index].communication = body;
    return {
      code: 200,
      message: '设置成功',
    };
  }

  async postParamSet(body: any) {
    const index = this.commInfo.findIndex((item) => item.id === body.id);
    this.commInfo[index].param = body;
    return {
      code: 200,
      message: '设置成功',
    };
  }

  async postManageSet(body: any) {
    if (!body.id) {
      body.id = Math.floor(Math.random() * 1000000);
      /* body.status = {
        id: 0,
        mode: 'on',
        status: 'normal',
        buc_warn: 'abnormal',
        bdc_warn: 'abnormal',
        type: '0.9double_paraboloid',
        temp: 0,
        elevate: 0,
        azimuth: 0,
        inertial_navigate_warn: 'abnormal',
        overtemp_warn: 'abnormal',
        transmit_front_status_1: 'abnormal',
        transmit_front_status_2: 'abnormal',
        transmit_front_status_3: 'abnormal',
        transmit_front_status_4: 'abnormal',
        receive_front_status_1: 'abnormal',
        receive_front_status_2: 'abnormal',
        receive_front_status_3: 'abnormal',
        receive_front_status_4: 'abnormal',
      } */
      this.commInfo.push(body);
    } else {
      const index = this.commInfo.findIndex((item) => item.id === body.id);
      this.commInfo[index] = body;
    }
    return {
      code: 200,
      message: '设置成功',
    };
  }

  async postDel(body: any) {
    const index = this.commInfo.findIndex((item) => item.id === body.id);
    this.commInfo.splice(index, 1);
    return {
      code: 200,
      message: '删除成功',
    };
  }

  public antennaStatus = [
    {
      id: 0,
      status: 'on',
    },
    {
      id: 1,
      status: 'off',
    },
  ];
  private antennaStatusIndex = 0;
  setAntennaStatus() {
    this.antennaStatusIndex++;
    if (this.antennaStatusIndex % 4 === 0) {
      this.antennaStatus[0].status = 'on';
      this.antennaStatus[1].status = 'off';
    } else {
      this.antennaStatus[0].status = 'off';
      this.antennaStatus[1].status = 'on';
    }
    return this.antennaStatus;
  }

  async postStatusGet() {
    return {
      code: 200,
      data: {
        state: this.setAntennaStatus(),
      },
    };
  }

  private antStatus = [
    {
      id: 1,
      buc_warn: 'abnormal',
      bdc_warn: 'abnormal',
      temp: 'string1',
      inertial_navigate_warn: 'abnormal',
      overtemp_warn: 'abnormal',
      transmit_front_status_1: 'abnormal',
      transmit_front_status_2: 'abnormal',
      transmit_front_status_3: 'abnormal',
      transmit_front_status_4: 'abnormal',
      receive_front_status_1: 'abnormal',
      receive_front_status_2: 'abnormal',
      receive_front_status_3: 'abnormal',
      receive_front_status_4: 'abnormal',
    },
    {
      id: 2,
      buc_warn: 'normal',
      bdc_warn: 'normal',
      temp: 'normal',
      inertial_navigate_warn: 'normal',
      overtemp_warn: 'normal',
      transmit_front_status_1: 'normal',
      transmit_front_status_2: 'normal',
      transmit_front_status_3: 'normal',
      transmit_front_status_4: 'normal',
      receive_front_status_1: 'normal',
      receive_front_status_2: 'normal',
      receive_front_status_3: 'normal',
      receive_front_status_4: 'normal',
    },
  ];

  async postPhasedArrayStatus(body: any) {
    return {
      code: 200,
      message: '设置成功',
      data: {
        ant: this.antStatus,
      },
    };
  }

  private faultStatus = [
    {
      attitude: 'normal',
      location: 'normal',
      time_service: 'normal',
      fan: 'normal',
      temp_0: 'normal',
      temp_1: 'normal',
      temp_2: 'normal',
      temp_3: 'normal',
      temp_4: 'normal',
      temp_5: 'normal',
      temp_6: 'normal',
      temp_7: 'normal',
      temp_8: 'normal',
      temp_9: 'normal',
      emission_current_1: 'normal',
      emission_current_2: 'normal',
      receive_current_1: 'normal',
      receive_current_2: 'normal',
      receive_current_3: 'normal',
      receive_current_4: 'normal',
      voltage_1: 'normal',
      voltage_2: 'normal',
      voltage_3: 'normal',
      voltage_4: 'normal',
      voltage_5: 'normal',
      voltage_6: 'normal',
      voltage_7: 'normal',
      voltage_8: 'normal',
      voltage_9: 'normal',
      voltage_10: 'normal',
      voltage_11: 'normal',
      transmit_front_status_1: 'abnormal',
      transmit_front_status_2: 'abnormal',
      transmit_front_status_3: 'abnormal',
      transmit_front_status_4: 'abnormal',
      receive_front_status_1: 'abnormal',
      receive_front_status_2: 'abnormal',
      receive_front_status_3: 'abnormal',
      ant_upgrade_type: '11',
      ant_upgrade_state: '11',
    },
    {
      attitude: 'abnormal',
      location: 'abnormal',
      time_service: 'abnormal',
      fan: 'normal',
      temp_0: 'normal',
      temp_1: 'normal',
      temp_2: 'normal',
      temp_3: 'normal',
      temp_4: 'normal',
      temp_5: 'normal',
      temp_6: 'normal',
      temp_7: 'normal',
      temp_8: 'normal',
      temp_9: 'normal',
      emission_current_1: 'normal',
      emission_current_2: 'normal',
      receive_current_1: 'normal',
      receive_current_2: 'normal',
      receive_current_3: 'normal',
      receive_current_4: 'normal',
      voltage_1: 'normal',
      voltage_2: 'normal',
      voltage_3: 'normal',
      voltage_4: 'normal',
      voltage_5: 'normal',
      voltage_6: 'normal',
      voltage_7: 'normal',
      voltage_8: 'normal',
      voltage_9: 'normal',
      voltage_10: 'normal',
      voltage_11: 'normal',
      transmit_front_status_1: 'abnormal',
      transmit_front_status_2: 'abnormal',
      transmit_front_status_3: 'abnormal',
      transmit_front_status_4: 'abnormal',
      receive_front_status_1: 'abnormal',
      receive_front_status_2: 'abnormal',
      receive_front_status_3: 'abnormal',
      ant_upgrade_type: '11',
      ant_upgrade_state: '11',
    },
  ];

  async postFaultStatus(body: any) {
    return {
      code: 200,
      message: '设置成功',
      data: {
        ant: this.faultStatus,
      },
    };
  }

  private antInsertionLossList = [
    {
      id: 1,
      value: 10,
    },
    {
      id: 2,
      value: 20,
    },
  ];

  async postAntInsertionLossGet() {
    return {
      code: 200,
      message: '获取成功',
      data: {
        list: this.antInsertionLossList,
      },
    };
  }

  async postAntInsertionLossSet(body: any) {
    this.antInsertionLossList = body.list;
    return {
      code: 200,
      message: '设置成功',
    };
  }

  async postAntBlockDataImport(body: any) {
    return {
      code: 200,
      message: '导入成功',
    };
  }

  public gfRealtimeInfo = {
    list: [
      {
        id: 0,
        send_freq: 0,
        send_gain: 0,
        recv_freq: 0,
        recv_gain: 0,
        current: 0,
        warn_status: 'off',
        gf_status: 'on',
        temperature: 20,
        fan_speed: 3,
        send_power: 30,
      },
      {
        id: 1,
        send_freq: 0,
        send_gain: 0,
        recv_freq: 0,
        recv_gain: 0,
        current: 0,
        warn_status: 'on',
        gf_status: 'on',
        temperature: 20,
        fan_speed: 3,
        send_power: 30,
      },
    ],
  };
  async postGfRealtimeInfo(body: any) {
    // 50%概率返回成功或失败
    const isSuccess = Math.random() < 0.5;

    if (isSuccess) {
      // 生成随机数据
      const randomData = {
        list: [
          {
            id: 0,
            send_freq: Math.floor(Math.random() * 10000),
            send_gain: Math.floor(Math.random() * 50),
            recv_freq: Math.floor(Math.random() * 10000),
            recv_gain: Math.floor(Math.random() * 50),
            current: Math.floor(Math.random() * 100),
            warn_status: Math.random() < 0.5 ? 'off' : 'on',
            gf_status: Math.random() < 0.5 ? 'on' : 'off',
            temperature: Math.floor(Math.random() * 80),
            fan_speed: Math.floor(Math.random() * 10),
            send_power: Math.floor(Math.random() * 100),
          },
          {
            id: 1,
            send_freq: Math.floor(Math.random() * 10000),
            send_gain: Math.floor(Math.random() * 50),
            recv_freq: Math.floor(Math.random() * 10000),
            recv_gain: Math.floor(Math.random() * 50),
            current: Math.floor(Math.random() * 100),
            warn_status: Math.random() < 0.5 ? 'off' : 'on',
            gf_status: Math.random() < 0.5 ? 'on' : 'off',
            temperature: Math.floor(Math.random() * 80),
            fan_speed: Math.floor(Math.random() * 10),
            send_power: Math.floor(Math.random() * 100),
          },
        ],
      };

      return {
        code: 200,
        message: '获取成功',
        data: randomData,
      };
    } else {
      return {
        code: 422,
        message: '获取失败',
      };
    }
  }

  public pppoeInfo = {
    list: [
      {
        id: 0,
        send_freq_max: 5800,
        send_freq_min: 2400,
        send_gain_max: 22,
        send_gain_min: 10,
        recv_freq_max: 5800,
        recv_freq_min: 2400,
        recv_gain_max: 20,
        recv_gain_min: 8,
        version: 'V2.5.1',
        pn_num: 'AN-PN-202305001',
        sn_num: 'AN-SN-789456123',
      },
      {
        id: 1,
        send_freq_max: 6000,
        send_freq_min: 3000,
        send_gain_max: 25,
        send_gain_min: 12,
        recv_freq_max: 6000,
        recv_freq_min: 3000,
        recv_gain_max: 23,
        recv_gain_min: 10,
        version: 'V3.0.0',
        pn_num: 'AN-PN-202306015',
        sn_num: 'AN-SN-123789456',
      },
    ],
  };
  async postGfFixedInfo(body: any) {
    return {
      code: 200,
      message: '获取成功',
      data: this.pppoeInfo,
    };
  }

  async postBeampointSet(body: any) {
    await new Promise((resolve) => setTimeout(resolve, 3000));
    return {
      code: 200,
      message: '设置成功',
    };
  }

  public antVendorInfo = {
    ant_device_name: 'string',
    ant_vendor: 'string',
    ant_sn: 'string',
    ant_production_data: 'string',
    ant_version: 'string',
  };

  async postAntVendorInfoGet(body: any) {
    return {
      code: 200,
      message: '获取成功',
      data: this.antVendorInfo,
    };
  }
}
